<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php


class CUserComponent
{
    public $con;
    public $userSouce;
    private $_initialized = false;
    
    public function  __construct()
    {
    }


    
    public function getObject(){
        $option = new OptionViewForm();
        $userDbSource = $option->getOptionValue("user_db_source");
        $this->userSouce = $userDbSource;
        if (empty($userDbSource)){
            $this->userSouce = "owner";
        }
        if (empty($userDbSource) || $userDbSource == "owner"){
            return new COwnerUser();
        }elseif($userDbSource == "discuz"){
            return new CDiscuzUcUser();
        }elseif($userDbSource == "phpwind"){
            return new CPhpwindUser();
        }elseif($userDbSource == "wordpress"){
            return new CWordpressUser();
        }elseif($userDbSource == "connector"){
            return new CConnectorUser();
        }elseif($userDbSource == "smtp"){
            return new CSmtpUser();
        }elseif($userDbSource == "ldap"){
            return new CLdapUser();
        }
        return false;
    }


    
    public function storeUser($userData){
        $user_name = $userData['user_name'];
        $user = User::model()->find("user_name='{$user_name}'", array("user_name"=>$user_name));

        if (empty($user)){
            $user = new User();
            $user["user_uuid"]   = uniqid();
            $user["user_name"]   = $user_name;
            $salt                = CUtils::genRandomString(6);
            $user["salt"]        = $salt;
            $password            = CUtils::genRandomString(16);
            $user["user_pass"]   = CSecurity::signPasswd($password, $salt);
            $user["user_status"] = 1;
            $user->save();
        }

        $this->storeUserMeta($user, $userData);
        return $user;
    }


    
    public function storeUserMeta($user, $userData){
                if(array_key_exists('email', $userData) && strlen($userData['email'])){
                        $userMetaEmail = UserMeta::model()->find("user_id={$user['id']} and meta_key='email'", array("user_id"=>$user['id'] ,"meta_key"=>'email'));
            if (!empty($userMetaEmail)){
                if ($userMetaEmail['meta_value'] != $userData['email']){
                    $userMetaEmail['meta_value'] = $userData['email'];
                    $userMetaEmail->save();
                }
            }else{
                $userMeta = new UserMeta();
                $userMeta["user_id"]=$user["id"];
                $userMeta["meta_key"]="email";
                $userMeta["meta_value"]=$userData['email'];
                $userMeta->save();
            }
        }
    }
}
?>